package com.mingmen.supplier.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.mingmen.supplier.R;
import com.mingmen.supplier.activity.CommodityShopActivity;
import com.mingmen.supplier.activity.DingDanZongListActivity;
import com.mingmen.supplier.activity.GongNoticeListActivity;
import com.mingmen.supplier.activity.NewDingDanListActivity;
import com.mingmen.supplier.activity.RestaurantManagerListActivity;
import com.mingmen.supplier.activity.SupplierDetailActivity;
import com.mingmen.supplier.activity.WeiHuListActivity;
import com.mingmen.supplier.base.BaseBean;
import com.mingmen.supplier.convenientbanner.ConvenientBanner;
import com.mingmen.supplier.convenientbanner.holder.CBViewHolderCreator;
import com.mingmen.supplier.convenientbanner.listener.OnItemClickListener;
import com.mingmen.supplier.defineview.LocalImageHolderView;
import com.mingmen.supplier.http.callback.StringCallback;
import com.mingmen.supplier.httpconfig.HttpResponse;
import com.mingmen.supplier.immersionbar.ImmersionBar;
import com.mingmen.supplier.model.ShopCloseFlageModel;
import com.mingmen.supplier.util.GsonUtil;
import com.mingmen.supplier.util.SharePreferenceUtil;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;


public class HomeIndexFragment extends Fragment implements OnItemClickListener {


    Unbinder unbinder;
    @BindView(R.id.img_close)
    ImageView img_close;
    @BindView(R.id.img_bidian)
    ImageView img_bidian;
    @BindView(R.id.layout_close)
    LinearLayout layout_close;
    @BindView(R.id.layout_gongying)
    LinearLayout layout_gongying;
    @BindView(R.id.layout_commityShop)
    LinearLayout layout_commityShop;
    @BindView(R.id.layout_noteice)
    LinearLayout layout_noteice;
    @BindView(R.id.layout_weihu)
    LinearLayout layout_weihu;
    @BindView(R.id.layout_cantingmanager)
    LinearLayout layout_cantingmanager;
    @BindView(R.id.layout_dingdanlist)
    LinearLayout layout_dingdanlist;
    @BindView(R.id.layout_daipeisong)
    LinearLayout layout_daipeisong;
    @BindView(R.id.layout_yiwancheng)
    LinearLayout layout_yiwancheng;
    @BindView(R.id.layout_cancelOrder)
    LinearLayout layout_cancelOrder;

    List<String> dataList = new ArrayList<>();
    @BindView(R.id.convenientBanner)
    ConvenientBanner convenientBanner;
    private ArrayList<Integer> localImages = new ArrayList<Integer>();
    @BindView(R.id.layout_supplierdetail)
    LinearLayout layout_supplierdetail;
    @BindView(R.id.layout_huizong)
    LinearLayout layout_huizong;
    String token;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_refresh_practive, null);

        unbinder = ButterKnife.bind(this, view);

        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        localImages.clear();
        for (int i = 0; i < 4; i++) {
            dataList.add("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2534506313,1688529724&fm=26&gp=0.jpg");
            dataList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598501909230&di=8d85a03db2237d9ec821bb5ee32f2e52&imgtype=0&src=http%3A%2F%2Fa0.att.hudong.com%2F56%2F12%2F01300000164151121576126282411.jpg");
            dataList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598501909229&di=05638165ff1ac8683747737894eef9a0&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F22%2F59%2F19300001325156131228593878903.jpg");
            dataList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1598501909229&di=44f6bfd22eca3f7afa6df67f76e65ca8&imgtype=0&src=http%3A%2F%2Fp2.so.qhimgs1.com%2Ft01dfcbc38578dac4c2.jpg");
        }
        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");
        init();
//        getShopFlag();
        return view;

    }

    private void init() {
//        initImageLoader();
        loadTestDatas();
        //本地图片例子
        convenientBanner.setPages(
                new CBViewHolderCreator() {
                    @Override
                    public LocalImageHolderView createHolder(View itemView) {
                        return new LocalImageHolderView(itemView);
                    }

                    @Override
                    public int getLayoutId() {
                        return R.layout.item_localimage;
                    }
                }, localImages)
                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
//                .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
                .setPageIndicator(new int[]{R.drawable.shape_normal_intictor_white, R.drawable.shape_indictor_redjuxing})
                .setOnItemClickListener(this);
        convenientBanner.startTurning();
        //设置指示器的方向
//                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT)
//                .setOnPageChangeListener(this)//监听翻页事件
        ;
//        convenientBanner.setManualPageable(false);//设置不能手动影响
        //网络加载例子
//        networkImages=Arrays.asList(images);
//        convenientBanner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
//            @Override
//            public NetworkImageHolderView createHolder() {
//                return new NetworkImageHolderView();
//            }
//        },networkImages);


//手动New并且添加到ListView Header的例子
//        ConvenientBanner mConvenientBanner = new ConvenientBanner(this,false);
//        mConvenientBanner.setMinimumHeight(500);
//        mConvenientBanner.setPages(
//                new CBViewHolderCreator<LocalImageHolderView>() {
//                    @Override
//                    public LocalImageHolderView createHolder() {
//                        return new LocalImageHolderView();
//                    }
//                }, localImages)
//                //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
//                .setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused})
//                        //设置指示器的方向
//                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT)
//                .setOnItemClickListener(this);
//        listView.addHeaderView(mConvenientBanner);
    }

    private void loadTestDatas() {
        //本地图片集合
        for (int position = 0; position < 4; position++)
            localImages.add(getResId("ic_test_" + position, R.drawable.class));

////        //各种翻页效果
//        transformerList.add(DefaultTransformer.class.getSimpleName());
//        transformerList.add(AccordionTransformer.class.getSimpleName());
//        transformerList.add(BackgroundToForegroundTransformer.class.getSimpleName());
//        transformerList.add(CubeInTransformer.class.getSimpleName());
//        transformerList.add(CubeOutTransformer.class.getSimpleName());
//        transformerList.add(DepthPageTransformer.class.getSimpleName());
//        transformerList.add(FlipHorizontalTransformer.class.getSimpleName());
//        transformerList.add(FlipVerticalTransformer.class.getSimpleName());
//        transformerList.add(ForegroundToBackgroundTransformer.class.getSimpleName());
//        transformerList.add(RotateDownTransformer.class.getSimpleName());
//        transformerList.add(RotateUpTransformer.class.getSimpleName());
//        transformerList.add(StackTransformer.class.getSimpleName());
//        transformerList.add(ZoomInTransformer.class.getSimpleName());
//        transformerList.add(ZoomOutTranformer.class.getSimpleName());

//        transformerArrayAdapter.notifyDataSetChanged();
    }

    /**
     * 通过文件名获取资源id 例子：getResId("icon", R.drawable.class);
     *
     * @param variableName
     * @param c
     * @return
     */
    public static int getResId(String variableName, Class<?> c) {
        try {
            Field idField = c.getDeclaredField(variableName);
            return idField.getInt(idField);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public void getShopFlag() {
        HttpResponse.setShopCloseOrOpen(token, new StringCallback(getActivity()) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("setShopCloseOrOpen", s);
                BaseBean baseBean = new BaseBean();
                baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                if (baseBean.getCode() == 0) {
                    ShopCloseFlageModel shopCloseFlageModel = new ShopCloseFlageModel();
                    shopCloseFlageModel = GsonUtil.GsonToBean(s, ShopCloseFlageModel.class);
                    Toast.makeText(getActivity(), shopCloseFlageModel.getMessage() + "", Toast.LENGTH_SHORT).show();
                } else {

                }


            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.img_close, R.id.img_bidian, R.id.layout_close, R.id.layout_commityShop, R.id.layout_noteice, R.id.layout_weihu, R.id.layout_cantingmanager, R.id.layout_dingdanlist, R.id.layout_daipeisong, R.id.layout_yiwancheng, R.id.layout_cancelOrder, R.id.layout_supplierdetail, R.id.layout_huizong})
    public void onViewClicked(View view) {
        Intent intent;

        switch (view.getId()) {
            case R.id.img_close:
                layout_close.setVisibility(View.GONE);
                img_close.setVisibility(View.GONE);

                img_bidian.setVisibility(View.VISIBLE);
                layout_gongying.setVisibility(View.GONE);
                getShopFlag();

                break;
            case R.id.layout_supplierdetail:
                startActivity(new Intent(getActivity(), SupplierDetailActivity.class));
                break;
            case R.id.img_bidian:
                getShopFlag();
                img_close.setVisibility(View.VISIBLE);
                layout_gongying.setVisibility(View.VISIBLE);
                img_bidian.setVisibility(View.GONE);
                layout_close.setVisibility(View.VISIBLE);
                break;
            case R.id.layout_commityShop:
                startActivity(new Intent(getActivity(), CommodityShopActivity.class));

                break;
            case R.id.layout_noteice:
                startActivity(new Intent(getActivity(), GongNoticeListActivity.class));
                break;
            case R.id.layout_weihu:
                startActivity(new Intent(getActivity(), WeiHuListActivity.class));
                break;
            case R.id.layout_cantingmanager:
                startActivity(new Intent(getActivity(), RestaurantManagerListActivity.class));
                break;
            case R.id.layout_dingdanlist:
//                intent = new Intent(getActivity(), DingDanListActivity.class);
                intent = new Intent(getActivity(), NewDingDanListActivity.class);
                intent.putExtra("position", "0");
                startActivity(intent);
                break;
            case R.id.layout_daipeisong:
//                intent = new Intent(getActivity(), DingDanListActivity.class);
                intent = new Intent(getActivity(), NewDingDanListActivity.class);
                intent.putExtra("position", "1");
                startActivity(intent);
                break;
            case R.id.layout_yiwancheng:
                intent = new Intent(getActivity(), NewDingDanListActivity.class);
                intent.putExtra("position", "2");
                startActivity(intent);
                break;
            case R.id.layout_cancelOrder:
                intent = new Intent(getActivity(), NewDingDanListActivity.class);
                intent.putExtra("position", "3");
                startActivity(intent);
                break;
            case R.id.layout_huizong:
                startActivity(new Intent(getActivity(), DingDanZongListActivity.class));
                break;
        }
    }


    @Override
    public void onItemClick(int position) {
//        Toast.makeText(getActivity(), "点击了第" + position + "个", Toast.LENGTH_SHORT).show();
//        startActivity(new Intent(this, HeaderActivity.class));
//        convenientBanner.setCanLoop(!convenientBanner.isCanLoop());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        convenientBanner.stopTurning();
    }
}
